﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoShopping.Model;

namespace GoShopping.BLL.SysInfo
{
    /// <summary>
    /// 咨询操作类
    /// </summary>
    public class AdvisoryBean
    {
        public IList<Advisory> GetAllAdvisory()
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.Advisory).OrderBy(
                GoShoppingNBear.Advisory.ID.Desc
                ).ToList<Advisory>();
        }

        public IList<Advisory> GetAllAdvisory(int pageSize)
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.Advisory).SetSelectRange(
                pageSize,0,GoShoppingNBear.Advisory.ID)
                .OrderBy(GoShoppingNBear.Advisory.ID.Desc).ToList<Advisory>();
        }

        public IList<Advisory> GetAdvisoryList(string whereStr)
        {
            string sql = "SELECT * FROM " + GoShoppingNBear.Advisory.___GetTableName() + " WHERE 1=1 " + whereStr +
                " ORDER BY " + GoShoppingNBear.Advisory.ID.Name + " DESC";
            return Databases.DBGoShopping.CustomSql(sql).ToList<Advisory>();
        }

        /// <summary>
        /// 添加一个咨询
        /// </summary>
        /// <param name="adv"></param>
        /// <returns>返回添加的ID</returns>
        public int AddOneAdvisory(Advisory adv)
        {
            return Databases.DBGoShopping.Insert(GoShoppingNBear.Advisory).AddColumn(
                GoShoppingNBear.Advisory.Content, adv.Content).AddColumn(
                GoShoppingNBear.Advisory.CreateDate, DateTime.Now).AddColumn(
                GoShoppingNBear.Advisory.IsCover, adv.IsCover).AddColumn(
                GoShoppingNBear.Advisory.IsReply, adv.IsReply).AddColumn(
                //GoShoppingNBear.Advisory.ProductID, adv.ProductID).AddColumn(
                GoShoppingNBear.Advisory.ReplyContent, adv.ReplyContent).AddColumn(
                GoShoppingNBear.Advisory.ReplyDate, adv.ReplyDate).AddColumn(
                GoShoppingNBear.Advisory.ReplyerName, adv.ReplyerName).AddColumn(
                GoShoppingNBear.Advisory.Title, adv.Title).AddColumn(
                GoShoppingNBear.Advisory.UserName, adv.UserName).ExecuteReturnAutoIncrementID(
                GoShoppingNBear.Advisory.ID);
        }

        public void DelAdvisory(int advisoryID)
        {
            Databases.DBGoShopping.Delete(GoShoppingNBear.Advisory).Where(
                GoShoppingNBear.Advisory.ID == advisoryID).Execute();
        }

        /// <summary>
        /// 回复一条咨询
        /// </summary>
        /// <param name="advisoryID"></param>
        /// <param name="replyContent"></param>
        /// <param name="replyerName"></param>
        public void ReplyOneAdvisory(int advisoryID, string replyContent,string replyerName)
        {
            Databases.DBGoShopping.Update(GoShoppingNBear.Advisory).Where(
                GoShoppingNBear.Advisory.ID == advisoryID).AddColumn(
                GoShoppingNBear.Advisory.IsReply, true).AddColumn(
                GoShoppingNBear.Advisory.ReplyDate, DateTime.Now).AddColumn(
                GoShoppingNBear.Advisory.ReplyContent, replyContent).AddColumn(
                GoShoppingNBear.Advisory.ReplyerName, replyerName).Execute();
        }

        /// <summary>
        /// 屏蔽/还原一条评论
        /// </summary>
        /// <param name="advisoryID"></param>
        /// <param name="covered"></param>
        public void CoverOneAdvisory(int advisoryID,bool covered)
        {
            Databases.DBGoShopping.Update(GoShoppingNBear.Advisory).Where(
                GoShoppingNBear.Advisory.ID == advisoryID).AddColumn(
                GoShoppingNBear.Advisory.IsCover, covered).Execute();
        }
    }
}
